package com.sskj.spk.ui.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.box.imageloader.GlideImageLoader;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.spk.R;
import com.sskj.spk.bean.MainBannerBean;
import com.sskj.spk.presenter.HangQingFragmentPresenter;
import com.sskj.spk.ui.activity.BannerWebActivity;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@Route(path = RConfig.APP_HANGQING)//行情主页面
public class HangQingFragment extends BaseFragment<HangQingFragmentPresenter> {
    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tvNotice)
    TextSwitcher tvNotice;
    @BindView(R.id.hq_gg_ll)
    LinearLayout hqGgLl;  // 公告
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private Disposable noticeDispo;
    private MainBannerBean.ArticleBean noticeList;


    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_hangqing;
    }

    @Override
    public HangQingFragmentPresenter getPresenter() {
        return new HangQingFragmentPresenter();
    }

    @Override
    public void initView() {
        titles.add("BDD");
        titles.add("USDT");
        Flowable.fromIterable(titles)
                .onBackpressureDrop()
                .subscribe(item -> fragments.add(MarketListHangqingFragment.newInstance(item, false)));
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), titles, fragments));
        slidingTabLayout.setViewPager(viewPager);

        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        tvNotice.setFactory(() -> {
            TextView textView = new TextView(getActivity());
            textView.setTextColor(getResources().getColor(R.color.app_99a));
            return textView;
        });
//        ClickUtil.click(tvNotice, () -> {
//            if (noticeList != null) {
//                ARouter.getInstance().build(RConfig.GONGGAO_DETAIL)
//                        .withString("noticeId", noticeList.getId())
//                        .navigation();  // 使用路由跳转
//            }
//        });
        ClickUtil.click(hqGgLl, () -> {
            ARouter.getInstance().build(RConfig.GONGGAO_ACTIVITY_MAIN).navigation();
        });
    }

    @Override
    public void initData() {
        mPresenter.getBannerUrl();
    }

    /**
     * 接口回调，显示Banner图
     */
    public void showBanner(MainBannerBean bannerBean) {
        if (this.banner == null)
            return;

        this.banner.setOnBannerListener(position -> {
            if (TextUtils.isEmpty(bannerBean.getBanner().get(position).getUrl())) {
                return;
            }
            BannerWebActivity.start(getActivity(), bannerBean.getBanner().get(position).getUrl());

        });
        if (tvNotice != null) {
            DisposUtil.close(noticeDispo);
            noticeDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
                    .onBackpressureDrop()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(i -> {
                        if (tvNotice != null) {
                            noticeList = bannerBean.getArticle().get((int) (i % bannerBean.getArticle().size()));
                            tvNotice.setText(noticeList.getTitle());
                        }
                    }, System.out::println);
            ;
            banner.setImages(bannerBean.getBanner());
        }
        this.banner.start();


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(noticeDispo);
    }

    class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            ImageUtil.setImage(((MainBannerBean.BannerBean) path).getBanner_url(), imageView);
        }
    }
}
